package bjx.com.kehu.ui.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import bjx.com.kehu.R;
import bjx.com.kehu.adapter.TradeHisAdapter;
import bjx.com.kehu.contants.Constants;
import bjx.com.kehu.model.TradeHisModel;
import bjx.com.kehu.utils.GsonUtil;
import bjx.com.kehu.utils.MD5;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import cn.bingoogolapple.refreshlayout.BGAMoocStyleRefreshViewHolder;
import cn.bingoogolapple.refreshlayout.BGARefreshLayout;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by jqc on 2017/11/24.
 */

public class TradeHisActivity extends BaseActivity implements BGARefreshLayout.BGARefreshLayoutDelegate {

    ImageView mIVBack;
    TextView mTVTitle;

    BGARefreshLayout mRefreshLayout;
    ListView mListView;
    TradeHisAdapter mAdapter;

    int mCurrentPage = 1;
    String pageCount = "10";

    @Override
    protected void initView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_tradehis);
        mIVBack = getViewById(R.id.back);
        mTVTitle = getViewById(R.id.title);
        mTVTitle.setText("交易记录");

        mRefreshLayout = getViewById(R.id.rl_tradehis_refresh);
        mListView = getViewById(R.id.tradehis_lv);
        mAdapter = new TradeHisAdapter(mApp, R.layout.lv_item_tradehis);

        BGAMoocStyleRefreshViewHolder moocStyleRefreshViewHolder = new BGAMoocStyleRefreshViewHolder(mApp, true);
        moocStyleRefreshViewHolder.setUltimateColor(R.color.red);
        moocStyleRefreshViewHolder.setOriginalImage(R.mipmap.custom_mooc_icon);
        moocStyleRefreshViewHolder.setRefreshViewBackgroundColorRes(R.color.grey_light);
        moocStyleRefreshViewHolder.setLoadMoreBackgroundColorRes(R.color.grey_light);
        moocStyleRefreshViewHolder.setSpringDistanceScale(0);
        mRefreshLayout.setRefreshViewHolder(moocStyleRefreshViewHolder);

//        BGANormalRefreshViewHolder normalRefreshViewHolder = new BGANormalRefreshViewHolder(mApp, true);
//        mRefreshLayout.setRefreshViewHolder(normalRefreshViewHolder);
    }

    @Override
    protected void setListener() {
        mIVBack.setOnClickListener(this);
        mRefreshLayout.setDelegate(this);
    }

    @Override
    protected void processLogic(Bundle savedInstanceState) {
        mListView.setAdapter(mAdapter);
        mEngine.loadTradeHis(userModel.getUser_id(), MD5.getMessageDigest((userModel.getUser_id() + Constants.BASE_KEY).getBytes()), String.valueOf(mCurrentPage), pageCount).enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                try {
                    String str = response.body().string();
                    JSONObject jo = new JSONObject(str);
                    int status = jo.getInt("status");
                    String msg = jo.getString("msg");
                    if (status == 200) {
                        JSONArray ja = jo.getJSONArray("result");
                        List<TradeHisModel> mList = new ArrayList<>();
                        for (int i = 0; i < ja.length(); i++) {
                            mList.add(GsonUtil.jsonToTradeHisBean(ja.getJSONObject(i).toString()));
                        }
                        mAdapter.setData(mList);
                    } else if (status == 201) {
                        showToast("用户不存在");
                    } else if (status == 100) {
                        showToast("获取列表失败");
                    } else {
                        showToast("获取列表异常");
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back:
                finish();
                break;
            default:break;
        }
    }

    @Override
    public void onBGARefreshLayoutBeginRefreshing(BGARefreshLayout refreshLayout) {
        mCurrentPage = 1;
        mEngine.loadTradeHis(userModel.getUser_id(), MD5.getMessageDigest((userModel.getUser_id() + Constants.BASE_KEY).getBytes()), String.valueOf(mCurrentPage), pageCount).enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                try {
                    String str = response.body().string();
                    JSONObject jo = new JSONObject(str);
                    int status = jo.getInt("status");
                    String msg = jo.getString("msg");
                    if (status == 200) {
                        JSONArray ja = jo.getJSONArray("result");
                        List<TradeHisModel> mList = new ArrayList<>();
                        for (int i = 0; i < ja.length(); i++) {
                            mList.add(GsonUtil.jsonToTradeHisBean(ja.getJSONObject(i).toString()));
                        }
                        mAdapter.setData(mList);
                    } else if (status == 201) {
                        showToast("用户不存在");
                    } else if (status == 100) {
                        showToast("获取列表失败");
                    } else {
                        showToast("获取列表异常");
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                mRefreshLayout.endRefreshing();
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                mRefreshLayout.endRefreshing();
            }
        });
    }

    @Override
    public boolean onBGARefreshLayoutBeginLoadingMore(BGARefreshLayout refreshLayout) {
        mCurrentPage++;
        mEngine.loadTradeHis(userModel.getUser_id(), MD5.getMessageDigest((userModel.getUser_id() + Constants.BASE_KEY).getBytes()), String.valueOf(mCurrentPage), pageCount).enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                try {
                    String str = response.body().string();
                    JSONObject jo = new JSONObject(str);
                    int status = jo.getInt("status");
                    String msg = jo.getString("msg");
                    if (status == 200) {
                        JSONArray ja = jo.getJSONArray("result");
                        List<TradeHisModel> mList = new ArrayList<>();
                        for (int i = 0; i < ja.length(); i++) {
                            mList.add(GsonUtil.jsonToTradeHisBean(ja.getJSONObject(i).toString()));
                        }
                        mAdapter.addMoreData(mList);
                    } else if (status == 201) {
                        showToast("用户不存在");
                        mCurrentPage--;
                    } else if (status == 100) {
                        showToast("获取列表失败");
                        mCurrentPage--;
                    } else {
                        showToast("获取列表异常");
                        mCurrentPage--;
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                mRefreshLayout.endLoadingMore();
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                mCurrentPage--;
                mRefreshLayout.endLoadingMore();
            }
        });
        return true;
    }
}
